package org.example.domain.device.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.device.feign.entity.DeviceInfoDTO;
import org.example.device.feign.service.DeviceRemoteService;
import org.example.device.kafka.event.DeviceNotifyKafkaEvent;
import org.example.device.kafka.produce.DeviceKafkaProducer;
import org.example.device.mqtt.event.DeviceNotifyMqttEvent;
import org.example.device.mqtt.producer.DeviceMqttProducer;
import org.example.device.rabbit.event.DeviceNotifyRabbitEvent;
import org.example.device.rabbit.produce.DeviceRabbitProducer;
import org.example.device.redis.entity.DeviceRedisCache;
import org.example.device.redis.service.DeviceRedisService;
import org.example.device.repository.entity.DeviceDTO;
import org.example.device.repository.service.DeviceService;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
public class DeviceDomainService {
    private final DeviceRemoteService deviceRemoteService;
    private final DeviceRedisService deviceRedisService;
    private final DeviceService deviceService;
    private final DeviceKafkaProducer deviceKafkaProducer;
    private final DeviceMqttProducer deviceMqttProducer;
    private final DeviceRabbitProducer deviceRabbitProducer;
    public DeviceDTO deviceHandle(String id, String groupId){
        DeviceRedisCache deviceRedisCache = deviceRedisService.queryDevice(id);
        DeviceDTO deviceDTO = DeviceDTO.builder().id(id).build();
        if(Objects.isNull(deviceRedisCache)){
             deviceDTO = deviceService.queryDevice(id);
        }
        DeviceInfoDTO deviceInfoDTO = deviceRemoteService.queryDeviceInfo(groupId);
        DeviceNotifyKafkaEvent deviceNotifyKafkaEvent = DeviceNotifyKafkaEvent.builder()
                .deviceId(id)
                .build();
        deviceKafkaProducer.deviceNotify("kafka-test", deviceNotifyKafkaEvent);
        DeviceNotifyMqttEvent deviceNotifyMqttEvent = DeviceNotifyMqttEvent.builder()
                .deviceId(id)
                .build();
        deviceMqttProducer.deviceNotify("device-mqtt", deviceNotifyMqttEvent);
        DeviceNotifyRabbitEvent deviceNotifyRabbitEvent = DeviceNotifyRabbitEvent.builder()
                .deviceId(id)
                .build();
        deviceRabbitProducer.deviceNotify("rabbit.test", "rabbit.test", deviceNotifyRabbitEvent);
        return deviceDTO;
    }
}
